package com.medical.user.controller;

import java.util.Arrays;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import com.alibaba.fastjson.JSON;
import com.medical.common.constant.RedisLoginKey;
import com.medical.common.utils.JwtUtil;
import io.jsonwebtoken.Claims;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

import com.medical.user.entity.MemberEntity;
import com.medical.user.service.MemberService;
import com.medical.common.utils.PageUtils;
import com.medical.common.utils.R;


import javax.servlet.http.HttpServletResponse;


/**
 * 
 *
 * @author wen
 * @email 1023076426@qq.com
 * @date 2021-08-31 19:10:05
 */
@RestController
@CrossOrigin
@RequestMapping("user/member")
public class MemberController {
    @Autowired
    private MemberService memberService;
    @Autowired
    private StringRedisTemplate redisTemplate;
    @Autowired
    private JwtUtil jwtUtil;
//    private final String DOCTOR_ROLE="doctor:login:";
//    private final String PATIENT_ROLE="patient:login:";
    //每次用户请求访问列表时都来到这个接口
    @RequestMapping("/toLogin")
    public R toLogin(@RequestParam("url")String url,
                     @RequestParam(value = "token",required = false)String token){
        //登录地址
        String login="http://127.0.0.1:5501/login.html";
        if(StringUtils.isNotEmpty(token)){
            //验权
            Claims claims = jwtUtil.parseJWT(token);
            if(claims.get("roles").equals("医生")){
                String s = redisTemplate.opsForValue().get(RedisLoginKey.DOCTOR_ROLE.keyPrefix+token);
                //验证token是否通过
                if(StringUtils.isNotEmpty(s)){
                    String rurl=url+"?token="+token;
                    return R.ok().put("url",rurl);
                }
            }else if(claims.get("roles").equals("患者")){
                String s = redisTemplate.opsForValue().get(RedisLoginKey.PATIENT_ROLE.keyPrefix+token);
                //验证token是否通过
                if(StringUtils.isNotEmpty(s)){
                    String rurl=url+"?token="+token;
                    return R.ok().put("url",rurl);
                }
            }
            return R.error().put("login",login);
        }
        return R.error().put("login",login);
    }
    //登录页面
    @RequestMapping(value = "logining",method = RequestMethod.POST)
    public R login(@RequestBody MemberEntity memberEntity,
                   HttpServletResponse response){
        String login="http://127.0.0.1:5501/login.html";
        MemberEntity memberEntity1=memberService.login(memberEntity);
        if (memberEntity1.getRole()==1){
            //表示医生 生成医生权限的令牌
            String token = jwtUtil.createJWT(memberEntity1.getId().toString(), memberEntity1.getUname(), "医生");
//            Cookie token1 = new Cookie("token", token);
//            response.addCookie(token1);//存入cookies
            String s = JSON.toJSONString(memberEntity1);
            redisTemplate.opsForValue().set(RedisLoginKey.DOCTOR_ROLE.keyPrefix+token,s,RedisLoginKey.DOCTOR_ROLE.expireTime, TimeUnit.MINUTES);//存入token中
            String rurl="http://127.0.0.1:5501/index_reception.html"+"?token="+token;
            return R.ok().put("url",rurl).put("token",token).put("user",memberEntity1);
        }else if (memberEntity1.getRole()==2){
            String token = jwtUtil.createJWT(memberEntity1.getId().toString(), memberEntity1.getUname(), "患者");
//            Cookie token1 = new Cookie("token", token);
//            response.addCookie(token1);//存入cookies
            String s = JSON.toJSONString(memberEntity1);
            redisTemplate.opsForValue().set(RedisLoginKey.PATIENT_ROLE.keyPrefix+token,s,RedisLoginKey.PATIENT_ROLE.expireTime, TimeUnit.MINUTES);//存入token中
            String rurl="http://127.0.0.1:5500/index.html"+"?token="+token;
            return R.ok().put("url",rurl).put("token",token).put("user",memberEntity1);
        }
        return R.error("登陆失败，请再次登录").put("login",login);
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = memberService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Integer id){
		MemberEntity member = memberService.getById(id);

        return R.ok().put("member", member);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody MemberEntity member){
		memberService.save(member);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody MemberEntity member){
		memberService.updateById(member);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Integer[] ids){
		memberService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
